/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.manager;

import com.ez2find.model.RegisterUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.adrian.encrypt.Encrypter;

/**
 *
 * @author Owner
 */
@Stateless
public class RegisterUserManagerBean implements RegisterUserManagerLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(RegisterUser registerUser) throws Exception {
        Encrypter encrypter = new Encrypter();
        String encryptedId = encrypter.encrypt(registerUser.getEmail());
        String r = "";
        for (int i = 0; i < encryptedId.length(); i++){
            if (encryptedId.charAt(i) != '+')
                r += encryptedId.charAt(i);
        }
        registerUser.setEncryptedId(r);
        em.persist(registerUser);
    }

    public void createUser(RegisterUser registerUser) throws Exception {
        Encrypter encrypter = new Encrypter();
        String encryptedId = encrypter.encrypt(registerUser.getEmail());
        String r = "";
        for (int i = 0; i < encryptedId.length(); i++) {
            if (encryptedId.charAt(i) != '+')
                r += encryptedId.charAt(i);
        }
        registerUser.setEncryptedId(r);
        String secure = encrypter.encrypt(registerUser.getPassword());
        registerUser.setPassword(secure);
        em.persist(registerUser);
    }
    
    public RegisterUser merge(RegisterUser registerUser) throws Exception {
        return em.merge(registerUser);
    }

    public void update(RegisterUser registerUser, boolean changePassword) throws Exception {
        if (changePassword) {
            Encrypter encrypter = new Encrypter();
            String secure = encrypter.encrypt(registerUser.getPassword());
            registerUser.setPassword(secure);
        }
        em.merge(registerUser);
    }

    public void delete(RegisterUser registerUser) throws Exception {
        registerUser = em.merge(registerUser);
        em.remove(registerUser);
    }

    public RegisterUser find(long id) {
        return em.find(RegisterUser.class, id);
    }

    public List<RegisterUser> listAll() {
        Query query = em.createNamedQuery("listAllRegisterUsers");
        List<RegisterUser> result = query.getResultList();
        return result;
    }

    public RegisterUser findLoginUser(String email, String pwd) {
        try {
            Encrypter e = new Encrypter();
            String securePwd = e.encrypt(pwd);
            Query query = em.createNamedQuery("loginUser");
            query.setParameter("email", email);
            query.setParameter("pwd", securePwd);
            RegisterUser result = (RegisterUser) query.getSingleResult();
            return result;
        } catch (Exception ex) {
            return null;
        }
    }

    public RegisterUser findUserForConfirmation(String encryptedId, String confirmationCode) {
        try {
            Query query = em.createNamedQuery("findUserForConfirmation");
            query.setParameter("encryptedId", encryptedId);
            query.setParameter("confirmationCode", confirmationCode);
            RegisterUser result = (RegisterUser) query.getSingleResult();
            return result;
        } catch (Exception ex) {
            return null;
        }
    }

    public RegisterUser findUserByEmail(String email) {
        try {
            Query query = em.createNamedQuery("findUserByEmail");
            query.setParameter("email", email);
            RegisterUser result = (RegisterUser) query.getSingleResult();
            return result;
        } catch (Exception ex) {
            return null;
        }
    }
        public void updateUser(RegisterUser registerUser)throws Exception {
        em.merge(registerUser);
    }
}
